﻿Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Net.Mail
Imports System.Text
Imports System.Text.RegularExpressions

Namespace Messaging

#Region "Enumerations "

    Public Enum MailRecipientType
        [TO]
        [CC]
        [BCC]
    End Enum

#End Region

    ''' <summary>
    ''' This class provides a standard way to send email from VIPdesk applications.
    ''' </summary>
    ''' <remarks>Be sure to configure System.Net.Mail in your app.config or web.config file.</remarks>
    Public Class Email

#Region "Properties and private members "

        Private _FROM As MailAddress
        Public Property [FROM]() As MailAddress
            Get
                Return _FROM
            End Get
            Set(ByVal value As MailAddress)
                _FROM = value
            End Set
        End Property

        Private _ReplyTo As MailAddress
        Public Property ReplyTo() As MailAddress
            Get
                Return _ReplyTo
            End Get
            Set(ByVal value As MailAddress)
                _ReplyTo = value
            End Set
        End Property

        Private _TO As MailAddressCollection
        Public Property [TO]() As MailAddressCollection
            Get
                If _TO Is Nothing Then
                    _TO = New MailAddressCollection
                End If
                Return _TO
            End Get
            Set(ByVal value As MailAddressCollection)
                _TO = value
            End Set
        End Property

        Private _CC As MailAddressCollection
        Public Property [CC]() As MailAddressCollection
            Get
                If _CC Is Nothing Then
                    _CC = New MailAddressCollection
                End If
                Return _CC
            End Get
            Set(ByVal value As MailAddressCollection)
                _CC = value
            End Set
        End Property

        Private _BCC As MailAddressCollection
        Public Property [BCC]() As MailAddressCollection
            Get
                If _BCC Is Nothing Then
                    _BCC = New MailAddressCollection
                End If
                Return _BCC
            End Get
            Set(ByVal value As MailAddressCollection)
                _BCC = value
            End Set
        End Property

        Private _Subject As String
        Public Property Subject() As String
            Get
                Return _Subject
            End Get
            Set(ByVal value As String)
                _Subject = value
            End Set
        End Property

        Private _MessageBody As String
        Public Property MessageBody() As String
            Get
                Return _MessageBody
            End Get
            Set(ByVal value As String)
                _MessageBody = value
            End Set
        End Property

        Private _FileNames As String()
        Public Property Attachments() As String()
            Get
                Return _FileNames
            End Get
            Set(ByVal value As String())
                _FileNames = value
            End Set
        End Property

        Private _IsHTML As Boolean
        Public Property IsHTML() As Boolean
            Get
                Return _IsHTML
            End Get
            Set(ByVal value As Boolean)
                _IsHTML = value
            End Set
        End Property

        Private _MailPriority As MailPriority
        Public Property [MailPriority]() As MailPriority
            Get
                Return _MailPriority
            End Get
            Set(ByVal value As MailPriority)
                _MailPriority = value
            End Set
        End Property

#End Region

#Region "Message Set-up methods "

        Public Sub AddToRecipients(ByVal SingleEmailAddress As String, ByVal DisplayName As String, ByVal RecipientType As MailRecipientType)
            Select Case RecipientType
                Case MailRecipientType.TO
                    If DisplayName.Length > 0 Then
                        Me.TO.Add(New MailAddress(SingleEmailAddress, DisplayName))
                    Else
                        Me.TO.Add(New MailAddress(SingleEmailAddress, DisplayName))
                    End If
                Case MailRecipientType.CC
                    If DisplayName.Length > 0 Then
                        Me.CC.Add(New MailAddress(SingleEmailAddress, DisplayName))
                    Else
                        Me.CC.Add(New MailAddress(SingleEmailAddress))
                    End If
                Case MailRecipientType.BCC
                    If DisplayName.Length > 0 Then
                        Me.BCC.Add(New MailAddress(SingleEmailAddress, DisplayName))
                    Else
                        Me.BCC.Add(New MailAddress(SingleEmailAddress))
                    End If
            End Select
        End Sub

        Public Sub AddToRecipients(ByVal ListOfEmailAddresses As String(), ByVal RecipientType As MailRecipientType)
            For Each s As String In ListOfEmailAddresses
                Select Case RecipientType
                    Case MailRecipientType.TO
                        Me.TO.Add(New MailAddress(s))
                    Case MailRecipientType.CC
                        Me.CC.Add(New MailAddress(s))
                    Case MailRecipientType.BCC
                        Me.BCC.Add(New MailAddress(s))
                End Select
            Next
        End Sub

#End Region

#Region "Send Methods "

        Public Sub Send()

            If Me.TO.Count > 0 Then
                Dim mMailMessage As New MailMessage()

                'add sender (FROM)
                mMailMessage.From = Me.FROM

                ' add recipients (TO, CC, BCC)
                Dim recipient As MailAddress
                For Each recipient In Me.TO
                    mMailMessage.To.Add(recipient)
                Next
                If Me.CC IsNot Nothing Then
                    For Each recipient In Me.CC
                        mMailMessage.CC.Add(recipient)
                    Next
                End If
                If Me.BCC IsNot Nothing Then
                    For Each recipient In Me.BCC
                        mMailMessage.Bcc.Add(recipient)
                    Next
                End If

                If Me.ReplyTo IsNot Nothing Then
                    mMailMessage.ReplyToList.Add(Me.ReplyTo)
                End If

                ' Set the subject of the mail message
                mMailMessage.Subject = Me.Subject

                ' Set the body of the mail message
                mMailMessage.Body = Me.MessageBody

                ' Set the format of the mail message body as HTML
                mMailMessage.IsBodyHtml = Me.IsHTML
                mMailMessage.BodyEncoding = System.Text.Encoding.ASCII

                ' Set the priority of the mail message to normal
                mMailMessage.Priority = Me.MailPriority

                If Me.Attachments IsNot Nothing Then
                    If Me.Attachments.Length > 0 Then
                        For Each f As String In Me.Attachments
                            If f.Length > 0 Then
                                mMailMessage.Attachments.Add(New Attachment(f))
                            End If

                        Next
                    End If
                End If

                ' Instantiate a new instance of SmtpClient
                Dim mSmtpClient As New SmtpClient()
                'mSmtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis

                ' Send the mail message
                mSmtpClient.Send(mMailMessage)
            End If
        End Sub

        Public Shared Sub SendEmail(ByVal [From] As String, _
                                    ByVal [To] As String, _
                                    ByVal [Cc] As String, _
                                    ByVal [Bcc] As String, _
                                    ByVal Subject As String, _
                                    ByVal [Message] As String, _
                                    ByVal IsHtml As Boolean)
            Try
                Dim MyMessage As New Email()
                MyMessage.FROM = New MailAddress([From])
                MyMessage.TO.Add(New MailAddress([To]))
                If Cc IsNot Nothing Then
                    If Cc.Length > 0 Then
                        MyMessage.CC.Add(New MailAddress([Cc]))
                    End If
                End If
                If Bcc IsNot Nothing Then
                    If Bcc.Length > 0 Then
                        MyMessage.BCC.Add(New MailAddress([Bcc]))
                    End If
                End If
                MyMessage.Subject = Subject
                MyMessage.MessageBody = Message
                MyMessage.IsHTML = IsHtml
                MyMessage.Send()
            Catch ex As Exception
                Throw ex
            End Try
        End Sub

        Public Shared Sub SendEmail(ByVal [From] As String, _
                                ByVal [To] As String(), _
                                ByVal [Cc] As String(), _
                                ByVal [Bcc] As String(), _
                                ByVal Subject As String, _
                                ByVal [Message] As String, _
                                ByVal IsHtml As Boolean)
            Try
                Dim MyMessage As New Email()
                Dim recipient As String

                MyMessage.FROM = New MailAddress([From])

                For Each recipient In [To]
                    MyMessage.TO.Add(New MailAddress(recipient))
                Next

                If Cc IsNot Nothing Then
                    For Each recipient In [Cc]
                        If recipient.Length > 0 Then
                            MyMessage.CC.Add(New MailAddress([recipient]))
                        End If
                    Next
                End If

                If Bcc IsNot Nothing Then
                    For Each recipient In [Bcc]
                        If recipient.Length > 0 Then
                            MyMessage.BCC.Add(New MailAddress([recipient]))
                        End If
                    Next
                End If

                MyMessage.Subject = Subject
                MyMessage.MessageBody = Message
                MyMessage.IsHTML = IsHtml
                MyMessage.Send()
            Catch ex As Exception
                Throw ex
            End Try
        End Sub

        Public Shared Sub SendEmailWithAttachment(ByVal [From] As String, _
                                ByVal [To] As String, _
                                ByVal [Cc] As String, _
                                ByVal [Bcc] As String, _
                                ByVal Subject As String, _
                                ByVal [Message] As String, _
                                ByVal IsHtml As Boolean, _
                                ByVal FileName As String)
            Try
                Dim MyMessage As New Email()
                MyMessage.FROM = New MailAddress([From])
                MyMessage.TO.Add(New MailAddress([To]))

                If Cc IsNot Nothing Then
                    If Cc.Length > 0 Then
                        MyMessage.CC.Add(New MailAddress([Cc]))
                    End If
                End If

                If Bcc IsNot Nothing Then
                    If Bcc.Length > 0 Then
                        MyMessage.BCC.Add(New MailAddress([Bcc]))
                    End If
                End If

                MyMessage.Subject = Subject
                MyMessage.MessageBody = Message
                MyMessage.IsHTML = IsHtml

                If FileName.Length > 0 Then
                    If File.Exists(FileName) Then
                        Dim f(0) As String
                        f(0) = FileName
                        MyMessage.Attachments = f
                    End If
                End If

                MyMessage.Send()
            Catch ex As Exception
                Throw ex
            End Try
        End Sub

        Public Shared Sub SendEmailWithAttachment(ByVal [From] As String, _
                            ByVal [To] As String(), _
                            ByVal [Cc] As String(), _
                            ByVal [Bcc] As String(), _
                            ByVal Subject As String, _
                            ByVal [Message] As String, _
                            ByVal IsHtml As Boolean, _
                            ByVal FileNames As String())
            Try
                Dim MyMessage As New Email()
                Dim recipient As String

                MyMessage.FROM = New MailAddress([From])

                For Each recipient In [To]
                    MyMessage.TO.Add(New MailAddress(recipient))
                Next

                If Cc IsNot Nothing Then
                    For Each recipient In [Cc]
                        If recipient.Length > 0 Then
                            MyMessage.CC.Add(New MailAddress([recipient]))
                        End If
                    Next
                End If

                If Bcc IsNot Nothing Then
                    For Each recipient In [Bcc]
                        If recipient.Length > 0 Then
                            MyMessage.BCC.Add(New MailAddress([recipient]))
                        End If
                    Next
                End If

                MyMessage.Subject = Subject
                MyMessage.MessageBody = Message
                MyMessage.IsHTML = IsHtml

                If FileNames IsNot Nothing Then
                    If FileNames.Length > 0 Then
                        MyMessage.Attachments = FileNames
                    End If
                End If

                MyMessage.Send()
            Catch ex As Exception
                Throw ex
            End Try
        End Sub

#End Region

    End Class

End Namespace
